/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.DateUtil;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.security.util.SecurityUtil;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.dto.ExaminationRecordDto;
import com.shanhesoft.java.modules.exam.domain.dto.ExaminationRecordQueryCriteria;
import com.shanhesoft.java.modules.exam.service.ExaminationRecordService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.Date;
import java.util.Set;

/**
 * 考试记录管理Controller 考试记录管理
 * @author admin
 * @version 2020-10-10 17:00:18
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/examinationRecord")
@Slf4j
@AllArgsConstructor
public class ExaminationRecordResource extends BaseResource {

	private final ExaminationRecordService service;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
//	@PreAuthorize("@pms.hasPermission('exam_examinationRecord_view')")
	public Result get(@PathVariable String id) {
		if(StringUtils.isEmpty(id) || "undefined".equals(id)) {
			return Result.buildFail("请先回退到考试记录页面");
		}

		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.findExamRecordVobyId(id));
	}

	/**
	 * @param userId
	 * @return
	 */
	@GetMapping("/getWrongRecordsByUserId")
	public Result getWrongRecordsByUserId(PageModel pm, ExaminationRecordQueryCriteria examinationRecordQueryCriteria) {
		return  Result.buildOkData(service.getWrongRecordsByUserId(pm, examinationRecordQueryCriteria));
	}

	/**
	 * GET / : get all examinationRecord.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all examinationRecord
	 */

//	@PreAuthorize("@pms.hasPermission('exam_examinationRecord_view')")
	@GetMapping
	@ApiOperation(value = "考试记录管理查看")
	public Result getPage(PageModel pm, ExaminationRecordQueryCriteria examinationRecordQueryCriteria) {
		return Result.buildOkData(service.findPage(pm, examinationRecordQueryCriteria));
	}

	/**
	 * POST / : Save a examinationRecordDto.
	 *
	 * @param examinationRecordDto the HTTP examinationRecord
	 */
	@PreAuthorize("@pms.hasPermission('exam_examinationRecord_edit')")
	@ApiOperation(value = "考试记录管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody ExaminationRecordDto examinationRecordDto) {
		log.debug("REST request to save ExaminationRecordDto : {}", examinationRecordDto);
		service.saveOrUpdate(examinationRecordDto);
		return Result.buildOk("保存考试记录管理成功");

	}

	/**
	 * POST / : Save a examinationRecordDto.
	 *
	 * @param examinationRecordDto the HTTP examinationRecord
	 */
	@ApiOperation(value = "保存并返回对象")
	@PostMapping("/saveReturn")
	public Result saveReturn(@Valid @RequestBody ExaminationRecordDto examinationRecordDto) {
		log.debug("REST request to save ExaminationRecordDto : {}", examinationRecordDto);
		return Result.buildOkData(service.saveOrUpdateReturn(examinationRecordDto));

	}

	/**
	 * DELETE //:ids : delete the "ids" ExaminationRecord.
	 *
	 * @param ids the id of the examinationRecord to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_examinationRecord_del')")
	@ApiOperation(value = "考试记录管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete ExaminationRecord: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除考试记录管理成功");
	}
}
